package com.swiftops.basic.sys.service.impl;

import com.swiftops.common.service.AbstractService;
import com.swiftops.common.util.ListUtil;
import com.swiftops.basic.sys.dao.SysUserRoleMapper;
import com.swiftops.basic.sys.model.SysUserRole;
import com.swiftops.basic.sys.service.SysUserRoleService;
import com.swiftops.basic.sys.vo.entity.SysUserRoleEntityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 用户角色关系
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysUserRoleServiceImpl extends AbstractService<SysUserRole> implements SysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;


    /**
     * 获取用户角色关系详细信息
     *
     * @param userId 用户角色关系USER_ID
     * @return 用户角色关系
     */
    @Override
    public List<SysUserRoleEntityVO> getSysUserRoleListByUserId(String userId) {
        Condition condition = new Condition(SysUserRole.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("userId", userId);
        List<SysUserRole> list = this.selectByCondition(condition);
        List<SysUserRoleEntityVO> voList = ListUtil.convertList(list, SysUserRoleEntityVO.class);
        return voList;
    }

    /**
     * 替换用户角色关系
     *
     * @param list 用户角色关系实体列表
     * @param userId 用户角色关系USER_ID
     */
    @Override
    public void replaceSysUserRole(String userId, List<SysUserRoleEntityVO> list) {
        Condition condition = new Condition(SysUserRole.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("userId", userId);
        this.deleteByCondition(condition);
        //
        if (ListUtil.isNotEmpty(list)) {
            List<SysUserRole> convertList = ListUtil.convertList(list, SysUserRole.class);
            this.sysUserRoleMapper.batchInsertSysUserRole(convertList);
        }
    }

}
